#include <windows.h>
#include "MySQL/mysql.h"

#include "CDb.h"
#include "CConsole.h"
#include "CUsableItem.h"

CUsableItem::CUsableItem(unsigned short itemID) {
	setId(itemID);
	setType(ITEM_USABLE);

	loadStats();
}

CUsableItem::~CUsableItem(void) {
}

void CUsableItem::loadStats() {
	MYSQL_RES * res;
	MYSQL_ROW row;
	char query[8192];
	unsigned long count;

	unsigned short newItemID = 0x0000;

	memset(query, 0, 8192);
	sprintf(query, "SELECT su.StackSize, iv.BaseSell FROM stats_usable AS su LEFT JOIN item_values AS iv ON (su.ItemID = iv.ItemID) WHERE su.ItemID = %i", this->getId());
	count = (int) strlen(query);
	MYSQL * mysqlConn=CDb::getConn(); //get conn
	mysql_real_query(mysqlConn, query, count);
	res = mysql_store_result(mysqlConn);
	CDb::pushConn(mysqlConn); //release conn
	if(!res) {
		CConsole::outDebOnly("Error looking up usable item stats.");
	} else if(mysql_num_rows(res)){
		row = mysql_fetch_row(res);
		setStackSize(atoi(row[0]));
		setSellPrice(atoi(row[1]));
	}
	mysql_free_result(res);
	this->setAttributes();
}